package com.getword.utils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DataUtil {
    /**
     * 将大纲列表转成大纲tree
     * [{id:1, name:'', pid:-1},{id:2,name:'',pid:-1},{id:2,name:'',pid:1},]
     * <p>
     * [{id:1, name:'', pid:-1,children:{id:2,name:'',pid:1}},
     * {id:2,name:'',pid:-1}]
     */
    @Deprecated
    public static List<Map<String, Object>> convertToTree(List<Map<String, Object>> source) {
        List<Map<String, Object>> result = new ArrayList<>();
        for (int i = 0; i < result.size(); i++) {
            if(source.get(i)!=null&&(int)source.get(i).get("pid") == -1){  // 一级
                Map<String, Object> tmp = source.get(i);
                result.add(tmp);
                source.set(i, null); // 删除元数据该位置的节点
                // 寻找下一层
                for(int j=0;j<result.size();j++){
                    if(source.get(j)!=null&&source.get(j).get("pid") == tmp.get("id")){
                        if(tmp.get("children")==null){
                            tmp.put("children", new ArrayList<Map<String, Object>>());
                        }
                        List<Map<String, Object>> children = (List<Map<String, Object>>) tmp.get("children");
                        children.add(source.get(j));
                    }
                }
            }
        }
//        for (Map<String, Object> map : source) {
//            int pid = (int) map.get("pid");
//            if (pid == -1) {
//                HashMap<String, Object> map1 = (HashMap<String, Object>) map;
//                result.add((Map<String, Object>) map1.clone());
//                source.remove(map);
//            }
//        }
//        for (Map<String, Object> map : source) {
//            int pid = (int) map.get("pid");
//            Map<String, Object> parent = findById(result, pid);
//            if (parent == null) {
//                continue;
//            }
//            HashMap<String, Object> map1 = (HashMap<String, Object>) map;
//            parent.put("children", (Map<String, Object>) map1.clone());
//            source.remove(map);
//        }
        return result;
    }

    // 根据节点id寻找节点
    private static Map<String, Object> findById(List<Map<String, Object>> list, int id) {
        for (Map<String, Object> map : list) {
            int cid = (int) map.get("id");
            if (cid == id) {
                return map;
            }
            Object children = map.get("children");
        }
        return null;
    }


    /**
     * 给文件大小换算成合适的单位,文件最大单位为G
     */
    public static String sizeOf(long bytes) {
        if (bytes < 1024) {
            return bytes + "B";
        }
        if (bytes >= 1024 && bytes / 1024 < 1024) {
            return String.format("%.2f", bytes / 1024.0) + "KB";
        }
        if (bytes / 1024 >= 1024 && bytes / (1024 * 1024) < 1024) {
            return String.format("%.2f", bytes / (1024 * 1024.0)) + "MB";
        }
        if (bytes / (1024 * 1024) >= 1024 && bytes / (1024 * 1024 * 1024) < 1024) {
            return String.format("%.2f", bytes / (1024 * 1024 * 1024.0)) + "GB";
        }
        return "数值不合法";
    }

    public static void main(String[] args) {



    }
}
